<template>
  <div ref="lineRef" style="width: 100%; height: 300px" />
</template>

<script>
import * as echarts from 'echarts'
import { mapState } from 'vuex'
import { getThemeValue } from '@/utils/theme_color.js'
import ResizeObserver from 'resize-observer-polyfill'
export default {
  data() {
    return {
      option: {
        color: ['#5c7bef', '#6ecfa3', '#fa9b37'],
        tooltip: {
          trigger: 'axis',
        },
        legend: {
          show: false,
        },
        grid: {
          left: '4%',
          right: '5%',
          bottom: '3%',
          containLabel: true,
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: ['2023-3-34 12:02:11', '2023-3-34 12:02:11'],
          axisLabel: {
            formatter(params) {
              const arr = params.split(' ')
              return `${arr[0]}\n ${arr[1]}`
            },
          },
        },
        yAxis: {
          type: 'value',
        },
        series: [
          {
            name: 'Email',
            type: 'line',
            stack: 'Total',
            smooth: true,
            data: [120, 132, 101, 134, 90, 230, 210],
          },
          {
            name: 'Union Ads',
            type: 'line',
            stack: 'Total',
            smooth: true,
            data: [220, 182, 191, 234, 290, 330, 310],
          },
          {
            name: 'Video Ads',
            type: 'line',
            stack: 'Total',
            smooth: true,
            data: [150, 232, 201, 154, 190, 330, 410],
          },
        ],
      },
    }
  },
  computed: {
    ...mapState({
      sideTheme: (state) => state.settings.sideTheme,
    }),
  },
  watch: {
    sideTheme: {
      handler(newVal) {
        this.$nextTick(() => {
          this.updateChart()
        })
      },
      immediate: true,
    },
  },
  mounted() {
    this.initChart()
    const resizeOb = new ResizeObserver((entries) => {
      for (const entry of entries) {
        echarts.getInstanceByDom(entry.target)?.resize()
      }
    })
    resizeOb.observe(this.$refs.lineRef)
    // 添加随页面变化而变化
    window.addEventListener('resize', () => {
      this.chartInstance.resize()
    })
  },
  destroyed() {
    window.removeEventListener('resize', () => {
      this.chartInstance.resize()
    })
  },
  methods: {
    initChart() {
      // 初始化对象
      this.chartInstance = echarts.init(this.$refs.lineRef)
      this.chartInstance.setOption(this.option, true)
      window.addEventListener('resize', () => {
        this.chartInstance.resize()
      })
    },
    getData(newValue) {
      this.option = {
        color: newValue.color,
        grid: {
          left: '2%',
          top: '14%',
          right: '2%',
          bottom: '2%',
          // 把x轴和y轴纳入 grid
          containLabel: true,
        },
        tooltip: {
          show: true,
        },
        legend: {
          show: true,
          type: 'plain',
          left: 0,
          top: '2%',
          orient: 'horizontal',
          data: newValue.legendName,
        },
        xAxis: {
          axisLabel: {
            color: 'rgba(0,0,0,.45)',
          },
          axisTick: {
            lineStyle: {
              color: 'rgba(0,0,0,.45)',
            },
          },
          axisLine: {
            lineStyle: {
              color: 'rgba(0,0,0,.45)',
            },
          },
          type: 'category',
          data: newValue.xAxis,
          boundaryGap: false,
        },
        yAxis: {
          axisLabel: {
            color: 'rgba(0,0,0,.4)',
          },
          axisLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
          splitArea: {
            show: false,
          },
          type: 'value',
          minInterval: 1,
        },
        series: [
          {
            name: '事件总数',
            data: newValue.value[0].value,
            type: 'line',
            smooth: false,
            areaStyle: {
              opacity: 0.09,
            },
          },
          {
            name: '已处置数据',
            data: newValue.value[1].value,
            type: 'line',
            smooth: false,
            areaStyle: {
              opacity: 0.09,
            },
          },
        ],
      }
      this.chartInstance.setOption(this.option, true)
      this.updateChart()
    },
    updateChart() {
      const titleThemeDark = getThemeValue('titleThemeDark')
      const DarkThemeStyle = {
        title: {
          textStyle: {
            color: titleThemeDark.titleColor,
          },
        },
        legend: {
          textStyle: {
            color: titleThemeDark.titleColor,
          },
        },
        xAxis: {
          axisLabel: {
            color: titleThemeDark.titleColor,
          },
          axisLine: {
            show: false,
          },
        },
        yAxis: {
          axisLabel: {
            color: titleThemeDark.titleColor,
          },
          splitLine: {
            lineStyle: {
              color: ' rgba(0,0,0,0.15)',
            },
          },
          axisLine: {
            show: false,
          },
        },
      }
      const titleThemeHigh = getThemeValue('titleThemeHigh')
      const HighThemeStyle = {
        title: {
          textStyle: {
            color: titleThemeHigh.titleColor,
          },
        },
        legend: {
          textStyle: {
            color: titleThemeHigh.titleColor,
          },
        },
        xAxis: {
          axisLabel: {
            color: 'rgba(0,0,0,0.45)',
          },
          axisLine: {
            show: false,
          },
        },
        yAxis: {
          axisLabel: {
            color: ' rgba(0,0,0,0.45)',
          },
          axisLine: {
            show: false,
          },
        },
      }
      if (this.sideTheme === 'theme-dark' || this.sideTheme === 'tech') {
        this.chartInstance.setOption(DarkThemeStyle)
      } else {
        this.chartInstance.setOption(HighThemeStyle)
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.visit-app {
  width: 100%;
  height: 100%;
}
</style>
